package com.rj.framework.component.tech.captcha;

import com.rj.framework.component.TechnicalComponent;
import com.rj.framework.context.Context;

/**
 * 验证码验证组件
 */
public class CaptchaVerificationComponent implements TechnicalComponent<Object, Boolean, Context<?>> {

    private final CaptchaServiceComponent captchaService;

    public CaptchaVerificationComponent(CaptchaServiceComponent captchaService) {
        this.captchaService = captchaService;
    }

    @Override
    public Boolean execute(Object request, Context<?> context) {
        // 从配置获取参数
        //String captchaId = context.getContext("captchaId", String.class);
        //String inputField = context.getContext("inputField", String.class);

        //if (captchaId == null || inputField == null) {
        //    return ComponentOutput.failure("MISSING_CONFIG", "缺少必要配置参数");
        //}
        // 获取用户输入的验证码
       /* Map<String, Object> requestData = (Map<String, Object>) context.getRequestData();
        String userInput = (String) requestData.get(inputField);
        
        if (userInput == null || userInput.isEmpty()) {
            return ComponentOutput.failure("MISSING_INPUT", "未提供验证码");
        }*/
       /* // 检查重试次数
        String attemptKey = "captcha_attempts:" + captchaId;
        Integer attempts = context.getTemp(attemptKey, Integer.class) ?? 0;

        if (attempts >= MAX_ATTEMPTS) {
            return ComponentOutput.failure("MAX_ATTEMPTS", "验证码尝试次数过多");
        }*/
        String captchaId = "", userInput = "";
        // 验证验证码
        return captchaService.verifyCaptcha(captchaId, userInput);

    }
}